#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using FreeOnlineApplication.Class.Repository;
using System.Collections;
using FreeOnlineApplication.Class.Entity;

#endregion

namespace FreeOnlineApplication
{
    public partial class AvailableRoomForm : Form
    {
        RoomMasterRepository m_roomMasterRep;
        CheckInRepository m_checkInRep;
        BookingRepository m_bookingRep;
        RoomTypeRepository m_roomtypeRep;


        IMainForm m_mainForm;
        public AvailableRoomForm(IMainForm mainForm)
        {
            InitializeComponent();
            m_roomMasterRep = new RoomMasterRepository(mainForm.Connection);
            m_checkInRep = new CheckInRepository(mainForm.Connection);
            m_bookingRep = new BookingRepository(mainForm.Connection);
            m_roomtypeRep = new RoomTypeRepository(mainForm.Connection);

            m_mainForm = mainForm;
///            InitDataGridColumn();
            comboBoxRoomType.DataSource = m_roomtypeRep.GetAll(m_mainForm.CurrentUnit.ID);
        }

        private void InitDataGridColumn()
        {
            dataGridView1.Columns.Clear();
            dataGridView1.Rows.Clear();

            RoomType rty = (RoomType)comboBoxRoomType.SelectedItem;
            if (rty == null) return;

            IList rms = m_roomMasterRep.GetAllByRoomType(rty.ID);

            dataGridView1.Columns.Add("Time","Tanggal");
            foreach (RoomMaster rm in rms)
            {
                int c = dataGridView1.Columns.Add(rm.ID.ToString(), rm.CODE);
                rm.ROOM_TYPE = rty;
                dataGridView1.Columns[c].Tag = rm;
            }
            DateTime start = new DateTime(dateTimePicker1.Value.Year,
                dateTimePicker1.Value.Month, dateTimePicker1.Value.Day,0,0,0);
            DateTime end = new DateTime(dateTimePicker1.Value.Year,
                dateTimePicker1.Value.Month, dateTimePicker2.Value.Day,23,59,0);
            int hours = Convert.ToInt16(numericUpDownRange.Value);
            for (DateTime dt = start; dt < end; dt = dt.Add(new TimeSpan(hours,0,0)))
            {
                ArrayList result = (ArrayList)m_checkInRep.GetCheckInInfo(rms, dt, m_mainForm.CurrentUnit.ID);
                int newrow = dataGridView1.Rows.Add(result.ToArray());
                dataGridView1[0, newrow].ReadOnly = true;
            }

            refreshColor();
        }
        public static void AddNumberToGrid(DataGridView dgrid)
        {
            for (int count = 0; (count <= (dgrid.Rows.Count - 1)); count++)
            {
                dgrid.Rows[count].HeaderCell.Value = string.Format((count + 1).ToString(), "0");
                dgrid.Rows[count].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
        }
        private void refreshColor()
        {
            dataGridView1.Columns[0].Width = 150;
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].Width = 50;
                for (int j = 0; j < dataGridView1.Rows.Count; j++)
                {
                    string val = dataGridView1[i, j].Value == null ? "" : dataGridView1[i, j].Value.ToString();
                    switch (val)
                    {
                        case "AVLBL":
                            dataGridView1[i, j].Style.BackColor = Color.Green;
                            break;
                        case "USED":
                            dataGridView1[i, j].Style.BackColor = Color.Red;
                            break;
                        case "BOOKED":
                            dataGridView1[i, j].Style.BackColor = Color.Orange;
                            break;
                    }
                }
            }
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            InitDataGridColumn();
        }
        private void comboBoxRoomType_SelectedIndexChanged(object sender, EventArgs e)
        {
            RoomType rt = (RoomType)comboBoxRoomType.SelectedItem;
            textBoxFacility.Text = "";
            if (rt == null) return;
            textBoxFacility.Text = rt.FACILITY;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedCells[0] == null) return;
            if (dataGridView1.SelectedCells[0].Value == null) return;
            string val = dataGridView1.SelectedCells[0].Value.ToString();
            if (val == "AVLBL")
            {
                menuItemBooking_Click(null, null);
            }
            if (val == "BOOKED")
            {
                menuItemBooking_Click(null, null);
            }
        }

        private void menuItemBooking_Click(object sender, EventArgs e)
        {
            int t = dataGridView1.SelectedCells[0].RowIndex;
            int c = dataGridView1.SelectedCells[0].ColumnIndex;
            DateTime dt = Convert.ToDateTime(dataGridView1[0, t].Value);
            RoomMaster rm = (RoomMaster)dataGridView1.Columns[c].Tag;
            BookingForm bookFrm = new BookingForm(UserControlFactory.BOOKING, m_mainForm,
                dt, rm);
            bookFrm.ShowDialog();
        }

        private void menuItemCheckIn_Click(object sender, EventArgs e)
        {
            int t = dataGridView1.SelectedCells[0].RowIndex;
            int c = dataGridView1.SelectedCells[0].ColumnIndex;
            DateTime dt = Convert.ToDateTime(dataGridView1[0, t].Value);
            RoomMaster rm = (RoomMaster)dataGridView1.Columns[c].Tag;
            string val = dataGridView1.SelectedCells[0].Value.ToString();
            Booking booking = null;
            if (val == "BOOKED")
            {
                booking = m_bookingRep.FindBookingByRoomMasterAndDate(rm, dt);
            }
            if (booking == null)
            {
                BookingForm bookFrm = new BookingForm(UserControlFactory.BOOKING, m_mainForm,
                               dt, rm);
                bookFrm.ShowDialog();
            }
            else
            {
                CheckInForm checkInFrm = new CheckInForm(UserControlFactory.CHECKIN, m_mainForm,
                    booking);
                checkInFrm.ShowDialog();
            }
        }

        private void contextMenu1_Popup(object sender, EventArgs e)
        {
            menuItemBooking.Enabled = false;
            menuItemCheckIn.Enabled = false;
            if (dataGridView1.SelectedCells[0] == null) return;
            if (dataGridView1.SelectedCells[0].Value == null) return;
            string val = dataGridView1.SelectedCells[0].Value.ToString();
            if (val == "AVLBL")
            {
                menuItemBooking.Enabled = true;
                menuItemCheckIn.Enabled = true;
                return;
            }
            if (val == "BOOKED")
            {
                menuItemCheckIn.Enabled = true;
                return;
            }
        }
    }
}